package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjw.zstu.entity.*;
import com.mjw.zstu.mapper.CompanyMapper;
import com.mjw.zstu.mapper.OrderMapper;
import com.mjw.zstu.mapper.ProductMapper;
import com.mjw.zstu.mapper.UserMapper;
import com.mjw.zstu.resultDto.JsonResult;
import com.sun.tools.corba.se.idl.constExpr.Or;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class OrderService {
    public static  String getOrderId(){
        int machineId=1;
        int hashCodev= UUID.randomUUID().hashCode();
        if(hashCodev<0){
            hashCodev=-hashCodev;
        }
        return machineId + String.format("%015d", hashCodev);
    }
    @Autowired
    UserMapper userMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    MessageService messageService;

    @Transactional
    public JsonResult create(Orderlist order){
//        int user_id=order.getUser_id();
        System.out.println(order);
//        User user=userMapper.selectById(user_id);
//        if(user.getStatus()==1){
//            return new JsonResult(500,"黑名单中","null");
//        }
        Product product=new Product();
        product.setId(order.getProduct_id());
        QueryWrapper<Product> productQueryWrapper=new QueryWrapper<>(product);
        Product product1=productMapper.selectOne(productQueryWrapper);
        if(product1.getStock()==0){
            return new JsonResult(500,"库存为0",null);
        }
        product.setStock(product1.getStock()-1);
        productMapper.updateById(product);
        System.out.println(getOrderId());
        order.setOrder_id(getOrderId());
        long t=System.currentTimeMillis();
        order.setCreate_time(t);
        order.setStatus(0);
        System.out.println(order);
        orderMapper.insert(order);
        return new JsonResult(200,"购买成功",null);
    }
    //买家订单
    public JsonResult getOrderList(int company_id,int status){
       List<Map> l= orderMapper.getCompanyOrder(company_id,status);
        System.out.println(l);
       for(int i=0;i<l.size();i++){
           if(l.get(i).containsKey("user_id")){
               User user=new User();
               user.setUser_id((int)l.get(i).get("user_id"));
               QueryWrapper<User> userQueryWrapper=new QueryWrapper<>(user);
               userQueryWrapper.select(User.class,j->!j.getColumn().equals("password"));
               User user1=userMapper.selectOne(userQueryWrapper);
               l.get(i).put("user",user1);
           }else if(l.get(i).containsKey("company_id")){
               Company company=new Company();
               company.setCompany_id((int)l.get(i).get("company_id"));
               QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>(company);
               companyQueryWrapper.select(Company.class,j->!j.getColumn().equals("password"));
               Company company1=companyMapper.selectOne(companyQueryWrapper);
               l.get(i).put("company",company1);
           }

       }
       return new JsonResult(200,"获取成功",l);
    }
    //改变状态
    public JsonResult changeStatus(int id,int status){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setStatus(status);
       orderMapper.updateById(order);
       return new JsonResult(200,"操作成功",null);
    }
    public JsonResult getbuyerCompany(int id,int status){
        List l=orderMapper.getbuyerCompanyOrder(id,status);
        System.out.println(l);
        return new JsonResult(200,"获取成功",l);
    }

    public JsonResult getbuyerUser(int id,int status){
        List l=orderMapper.getbuyerUserOrder(id,status);
        return new JsonResult(200,"获取成功",l);
    }
    //发货
    public JsonResult fahuo(int  id,String track_number,String track_name   ){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setStatus(1);
        order.setTrack_name(track_name);
        order.setTrack_number(track_number);
        orderMapper.updateById(order);
        Orderlist o=orderMapper.selectById(id);
        Message message=new Message();
        long t=System.currentTimeMillis();
        message.setCreate_time(t);
        message.setMessage("订单:"+o.getOrder_id()+"，发货成功!");
        message.setReceiver(o.getUser_id()==null?o.getCompany_id():o.getUser_id());
        message.setIs_user(o.getUser_id()==null?0:1);
        message.setIs_read(0);
        messageService.inserts(message);
        return new JsonResult(200,"发货成功",null);
    }
    //收获
    public JsonResult shouhuo(int id){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setStatus(2);
        orderMapper.updateById(order);
        return new JsonResult(200,"收获成功",null);
    }
    //详情
    public JsonResult detail(int id){
        Map m=orderMapper.getdetail(id);
        Map m2=orderMapper.getseller(id);
        m.put("seller",m2);
        return new JsonResult(200,"",m);
    }
    //申请退款
    public JsonResult quit(int id,String reason){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setReason(reason);
        order.setStatus(3);
        orderMapper.updateById(order);
        return new JsonResult(200,"退款成功",null);
    }
    //评价
    public JsonResult star(int id,int star,String evaluate){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setStar(star);
        order.setEvaluate(evaluate);
        order.setStatus(5);
        orderMapper.updateById(order);

        return new JsonResult(200,"评价成功",null);
    }
    //确认退款
    public JsonResult confirm(int id){
        Orderlist order=new Orderlist();
        order.setId(id);
        order.setStatus(4);
        orderMapper.updateById(order);
        Orderlist o =  orderMapper.selectById(id);
        Message message=new Message();
        long t=System.currentTimeMillis();
        message.setCreate_time(t);
        message.setMessage("订单:"+o.getOrder_id()+"，退款成功!");
        message.setReceiver(o.getUser_id()==null?o.getCompany_id():o.getUser_id());
        message.setIs_user(o.getUser_id()==null?0:1);
        message.setIs_read(0);
        messageService.inserts(message);
        return new JsonResult(200,"退款成功",null);
    }
    //获取卖家订单
    public JsonResult getSellerOrder(int company_id,int status){
        List l= orderMapper.getCompanySalerOrder(company_id,status);
        return new JsonResult(200,"获取成功",l);
    }
    public JsonResult getalllist(int status){
        List<Map> l= orderMapper.all_list(status);
        for(int i=0;i<l.size();i++){
            if(l.get(i).containsKey("user_id")){
                User user=new User();
                user.setUser_id((int)l.get(i).get("user_id"));
                QueryWrapper<User> userQueryWrapper=new QueryWrapper<>(user);
                userQueryWrapper.select(User.class,j->!j.getColumn().equals("password"));
                User user1=userMapper.selectOne(userQueryWrapper);
                l.get(i).put("user",user1);
            }else if(l.get(i).containsKey("company_id")){
                Company company=new Company();
                company.setCompany_id((int)l.get(i).get("company_id"));
                QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>(company);
                companyQueryWrapper.select(Company.class,j->!j.getColumn().equals("password"));
                Company company1=companyMapper.selectOne(companyQueryWrapper);
                l.get(i).put("company",company1);
            }
        }
        return new JsonResult(200,"获取成功",l);
    }

}
